package com.jade.editor.anisotropy.parts;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

import com.jade.editor.anisotropy.node.Collector;

public class SettingAni extends Dialog {
	
	private int _xSize = 50;
	private int _ySize = 50;
	
	private int _primx;
	private int _primy;
	
	private int _xCount = 0;
	private int _yCount = 0;
	
	private RGB _borderColor = new RGB(0,0,0);
	private RGB _selectionColor = new RGB(255, 0, 0);
	private RGB _connectionColor = new RGB(0, 200, 10);

	private Collector _cl = null;
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public SettingAni(Shell parentShell, Point size, RGB borderColor, RGB selectionColor, RGB connectionColor, Collector cl) {
		super(parentShell);
		_xSize = size.x;
		_ySize = size.y;
		
		_primx = size.x;
		_primy = size.y;
		
		_borderColor = borderColor;
		_selectionColor = selectionColor;
		_connectionColor = connectionColor;
		
		_cl = cl;
	}
	
	public Point getSize(){
		if (_primx == _xSize && _primy == _ySize)
			return null;
		return new Point(_xSize, _ySize);
	}
	
	public RGB getBorderColor(){
		return _borderColor;
	}
	
	public RGB getSelectionColor(){
		return _selectionColor;
	}
	
	public RGB getConnectionColor(){
		return _connectionColor;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		final Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(null);
		
		Group grpSizeSettings = new Group(container, SWT.NONE);
		grpSizeSettings.setText("Size settings");
		grpSizeSettings.setBounds(10, 0, 290, 98);
		
		final Spinner xSpinner = new Spinner(grpSizeSettings, SWT.BORDER);
		xSpinner.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				_xSize = xSpinner.getSelection();
			}
		});
		xSpinner.setBounds(92, 22, 102, 22);
		xSpinner.setMinimum(50);
		xSpinner.setMaximum(2048);
		xSpinner.setSelection(_xSize);
		
		
		final Spinner ySpinner = new Spinner(grpSizeSettings, SWT.BORDER);
		ySpinner.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				_ySize = ySpinner.getSelection();
			}
		});
		ySpinner.setBounds(92, 50, 102, 22);
		ySpinner.setMinimum(50);
		ySpinner.setMaximum(2048);
		ySpinner.setSelection(_ySize);
		
		Label lblX = new Label(grpSizeSettings, SWT.NONE);
		lblX.setBounds(70, 25, 55, 15);
		lblX.setText("X:");
		
		Label lblY = new Label(grpSizeSettings, SWT.NONE);
		lblY.setBounds(70, 53, 55, 15);
		lblY.setText("Y:");
		
		Group grpColorSettings = new Group(container, SWT.NONE);
		grpColorSettings.setText("Color settings");
		grpColorSettings.setBounds(10, 104, 290, 114);
		
		final Composite colorsite = new Composite(grpColorSettings, SWT.NONE);
		colorsite.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				ColorDialog cd = new ColorDialog(container.getShell());
				RGB tmp = cd.open();
				if(tmp != null){
					_borderColor = tmp;
					colorsite.setBackground(SWTResourceManager.getColor(_borderColor));
				}
			}
		});
		colorsite.setBackground(SWTResourceManager.getColor(_borderColor));
		colorsite.setBounds(107, 25, 173, 19);
		
		final Composite selesite = new Composite(grpColorSettings, SWT.NONE);
		selesite.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				ColorDialog cd = new ColorDialog(container.getShell());
				RGB tmp = cd.open();
				if(tmp != null){
					_selectionColor = tmp;
					selesite.setBackground(SWTResourceManager.getColor(_selectionColor));
				}
			}
		});
		selesite.setBackground(SWTResourceManager.getColor(_selectionColor));
		selesite.setBounds(107, 50, 173, 19);
		
		final Composite conesite = new Composite(grpColorSettings, SWT.NONE);
		conesite.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				ColorDialog cd = new ColorDialog(container.getShell());
				RGB tmp = cd.open();
				if(tmp != null){
					_connectionColor = tmp;
					conesite.setBackground(SWTResourceManager.getColor(_connectionColor));
				}
			}
		});
		conesite.setBackground(SWTResourceManager.getColor(_connectionColor));
		conesite.setBounds(107, 75, 173, 19);
		
		Label lblColor = new Label(grpColorSettings, SWT.NONE);
		lblColor.setBounds(14, 25, 59, 15);
		lblColor.setText("Color:");
		
		Label lblSelection = new Label(grpColorSettings, SWT.NONE);
		lblSelection.setBounds(14, 50, 59, 15);
		lblSelection.setText("Selection:");
		
		Label lblConnection = new Label(grpColorSettings, SWT.NONE);
		lblConnection.setBounds(14, 75, 79, 15);
		lblConnection.setText("Connection:");
		
		Group grpPreviewSettings = new Group(container, SWT.NONE);
		grpPreviewSettings.setText("Preview settings");
		grpPreviewSettings.setBounds(10, 224, 290, 82);
		
		final Spinner xCountSpinner = new Spinner(grpPreviewSettings, SWT.BORDER);
		xCountSpinner.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				_xCount = xCountSpinner.getSelection();
			}
		});
		xCountSpinner.setBounds(80, 20, 47, 22);
		xCountSpinner.setMaximum(5);
		
		final Spinner yCountSpinner = new Spinner(grpPreviewSettings, SWT.BORDER);
		yCountSpinner.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				_yCount = yCountSpinner.getSelection();
			}
		});
		yCountSpinner.setBounds(80, 48, 47, 22);
		yCountSpinner.setMaximum(5);
		
		Label lblX_1 = new Label(grpPreviewSettings, SWT.NONE);
		lblX_1.setBounds(61, 23, 55, 15);
		lblX_1.setText("X:");
		
		Label lblY_1 = new Label(grpPreviewSettings, SWT.NONE);
		lblY_1.setBounds(61, 51, 55, 15);
		lblY_1.setText("Y:");
		
		Button peviewButton = new Button(grpPreviewSettings, SWT.NONE);
		peviewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new Preview(container.getShell(), _xCount, _yCount, _xSize, _ySize, _cl).open();
			}
		});
		peviewButton.setBounds(140, 20, 140, 52);
		peviewButton.setText("Preview");

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(317, 393);
	}
}
